package com.example.a7822.myapplication.shopcart.presenter;

import android.util.Log;

import com.example.a7822.myapplication.shopcart.beans.GoodBean;
import com.example.a7822.myapplication.shopcart.model.GoodModel;
import com.example.a7822.myapplication.shopcart.model.GoodModelImpl;
import com.example.a7822.myapplication.shopcart.view.GoodsView;

import java.util.List;

/**
 * Created by wp on 2016/6/14.
 */
public class GoodsPresenterImpl implements GoodsPresenter, GoodModelImpl.OnLoadGoodListListener,GoodModelImpl.OnLoadGoodListener {
    private static final String TAG = "GoodsPresenterImpl";
    private GoodModel goodModel;
    private GoodsView goodsView;

    public GoodsPresenterImpl(GoodsView goodsView) {
        this.goodsView = goodsView;
        goodModel = new GoodModelImpl();
    }

    @Override
    public void loadCart(int type, String pageIndex) {
        String url= "";//Urls.CONTROL_SERVER_ADDR+ String.format(Urls.PARAM_GET_NEWS_LIST,pageIndex,Urls.PAGE_SIZE);
        Log.d(TAG, url);
        //只有第一页的或者刷新的时候才显示刷新进度条
        if (pageIndex.equals("-1")) {
            goodsView.showProcessBar();
        }
        goodModel.loadCart(url, type, this);
    }

    @Override
    public void loadOneGood(String goodCode) {
//        OkHttpUtils.Param param1 = new OkHttpUtils.Param("shopId", String.valueOf(SPShopManager.curShop.getShopId()));
//        OkHttpUtils.Param param2 = new OkHttpUtils.Param("goodCode", goodCode);
//        List<OkHttpUtils.Param> list = new ArrayList<>();
//        list.add(param1);
//        list.add(param2);
//
//        goodModel.loadOneGood(Urls.GET_GOOD_INFO,list,this);
    }


    @Override
    public void onLoadGoodsSuccess(List<GoodBean> list) {
        goodsView.addGood(list);
        goodsView.dismissProcessBar();
    }

    @Override
    public void onLoadGoodsFailure(String msg, Exception e) {
        goodsView.dismissProcessBar();
        goodsView.showErrorMsg();
    }

    @Override
    public void onLoadGoodSuccess(GoodBean goodBean) {
        goodsView.dismissProcessBar();

//        ArrayList<GoodBean> list = new ArrayList<GoodBean>();
//        list.add(goodBean);
//
//        SPCartManager.getInstance().getGoodList().add(goodBean);
//        goodsView.addGood(list);
        goodsView.addGood(goodBean);

    }

    @Override
    public void onLoadGoodFailure(String msg, Exception e) {
        goodsView.dismissProcessBar();
        goodsView.showErrorMsg();
    }
}
